<?php
namespace Plu\Library\Models\Platform\Reports;

use Phalcon\Mvc\Model,
	Phalcon\Db\Column;

class ReportDailyRoomPlayTime extends Model {

    public function initialize() {
        $this->setConnectionService('db.report.slave');
    }

    public function getSource() {
        return "ReportDailyRoomPlayTime";
    }

    static public function getPlayHour($roomIds, $startTime, $endTime) {
        if (empty($roomIds)) {
            return false;
        }

        $sql = array();
        $len = count($roomIds);
        $params = array(
            'startTime' => $startTime,
            'endTime'   => $endTime
        );

        for ($c = 0; $c < $len; $c++) {
            $p = "r{$c}";
            $sql[] = " RoomId = :{$p}: ";
            $params[$p] = $roomIds[$c];
        }

        $sql = '(' . join(' OR ', $sql) . ')';

/*        $phql = "SELECT RoomId, sum(TotalSeconds) as TotalSeconds FROM Plu\Family\Models\Report\ReportDailyRoomPlayTime WHERE CategoryId = 0 AND Day >= :startTime: AND Day <= :endTime: AND " . $sql . " GROUP BY RoomId";

        $modelsManager = $this->getDI()->get('modelsManager');

        $items = $modelsManager->executeQuery($phql, $paramas);

        return $items;*/

        $items = self::sum(array(
            "conditions" => "CategoryId = 0 AND Day >= :startTime: AND Day <= :endTime: AND " . $sql,
            "column"     => "TotalSeconds",
            "group"      => "RoomId",
            "bind"       => $params
        ));

        return $items->toArray();
    }


}


?>
